﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WeMakeSP.Platform.SharePoint.BusinessEntities;
using WeMakeSP.SiteManagement.ServiceModel.Client;

namespace WeMakeSP.SiteManagement.UI.Models
{
    /// <summary>
    /// The base implementation for site management models.
    /// </summary>
    public class SiteManagementModelBase
    {
        #region Fields

        /// <summary>
        /// The services field.
        /// </summary>
        private SiteManagementServices servicesField;

        /// <summary>
        /// The site url.
        /// </summary>
        private string siteUrl;

        #endregion

        #region Constructors


        /// <summary>
        /// Initializes a new instance of the <see cref="SiteManagementModelBase"/> class.
        /// </summary>
        private SiteManagementModelBase()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SiteManagementModelBase"/> class.
        /// </summary>
        /// <param name="siteUrl">The site URL.</param>
        public SiteManagementModelBase(string siteUrl)
        {
            // Guard.

            if (string.IsNullOrEmpty(siteUrl))
            {
                throw new ArgumentNullException("siteUrl");
            }

            this.siteUrl = siteUrl;
        }

        #endregion

        #region Private Properties

        /// <summary>
        /// Gets the services.
        /// </summary>
        /// <value>The services.</value>
        protected SiteManagementServices Services
        {
            get
            {
                if (this.servicesField == null)
                {
                    this.servicesField = 
                        new SiteManagementServices(this.siteUrl);
                }

                return this.servicesField;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Gets the web configuration.
        /// </summary>
        /// <param name="webName">Name of the web.</param>
        /// <returns>A SharePoint web configuration.</returns>
        public SPWebConfiguration GetWebConfiguration(string webName)
        {
            return
                this.Services.GetWebConfiguration(webName);
        }

        /// <summary>
        /// Updates the master page.
        /// </summary>
        /// <param name="webId">The web id.</param>
        /// <param name="masterPage">The master page.</param>
        public void UpdateMasterPage(Guid webId, SPMasterPage masterPage)
        {
            this.Services.UpdateMasterPage(webId, masterPage);
        }

        /// <summary>
        /// Updates the web logo.
        /// </summary>
        /// <param name="webId">The web id.</param>
        /// <param name="webLogo">The web logo.</param>
        public void UpdateWebLogo(Guid webId, SPWebLogo webLogo)
        {
            this.Services.UpdateWebLogo(webId, webLogo);
        }

        /// <summary>
        /// Updates the web theme.
        /// </summary>
        /// <param name="webId">The web id.</param>
        /// <param name="theme">The theme.</param>
        public void UpdateWebTheme(Guid webId, string theme)
        {
            this.Services.UpdateWebTheme(webId, theme);
        }

        #endregion
    }
}
